export const validatePasswordStrength = (password: string) => {
  // let password: string = '';
  // if (typeof e === 'object' && e.target instanceof HTMLInputElement) password = e.target?.value;
  // if (typeof e === 'string') password = e;

  let score = 0;

  // check password length, <=4:5; 5-7:10; >=8:25
  if (password) {
    if (password.length <= 4) {
      score += 5;
    } else if (password.length >= 8) {
      score += 25;
    } else {
      score += 10;
    }
    if (password.length < 8) {
      return score;
    }
  }

  // check for letters, 大小写混合:20; 全是小(大)写:10; 没有字母:0
  if (/[a-z]/.test(password) && /[A-Z]/.test(password)) {
    score += 20;
  } else if (/[a-zA-Z]+/.test(password)) {
    score += 10;
  }

  // check for numbers, 大于等于2:20; 1:10; 没有:0
  const digitLength = (password.match(/\d/g) || []).length;

  if (digitLength >= 2) {
    score += 20;
  } else if (digitLength === 1) {
    score += 10;
  }

  // check for symbols, 大于等于2:25; 等于1:10; 0个:0
  const symbol_REG = /[!#%^&*()_+{}[\]:;<>,.?~\\/-]/g;
  const symbolLength = (password.match(symbol_REG) || []).length;

  if (symbolLength >= 2) {
    score += 25;
  } else if (symbolLength === 1) {
    score += 10;
  }

  // check for mixture, 大小写字母+数字+符号:5; 字母+数字+符号:3; 字母+数字:2
  // 字母+数字:2
  if (/[a-zA-Z]/.test(password) && /\d/.test(password)) {
    if (symbolLength >= 1) {
      score += 3;
    } else {
      score += 2;
    }
  }
  // 字母+数字+符号:3
  // if (/[a-zA-Z]/.test(password) && /\d/.test(password) && symbolLength >= 1) {
  //     score += 3
  // }
  // 大小写字母+数字+符号:5
  if (
    /[a-z]/.test(password) &&
    /[A-Z]/.test(password) &&
    /\d/.test(password) &&
    symbolLength >= 1
  ) {
    score += 5;
  }

  return score;
};
